<#include '/admin/header.html' > 
<#assign dateformat="com.enation.framework.directive.DateformateDirective"?new()>
<#assign permssion="com.enation.app.base.core.directive.PermssionDirective"?new()>

<style>
.input_text {
	width: 180px;
	height:25px;
}
.layui-input, .layui-textarea{
	display:inline-block;
	width:66%;
}
.layui-form-select .layui-edge{
	right:84px;
}
.layui-form-select dl{
    min-width: 66%;
}
.layui-table td, .layui-table th{
	padding:9px 0px;
}
.layui-input-block{
        margin-left:45%;
        margin-top: 36px;
}
#returngoodsdata thead tr{
    border: 1px solid rgba(204, 204, 204, 0.32);
    height:30px;
}
#returngoodsdata tbody tr{
    border: 1px solid rgba(204, 204, 204, 0.32);
    height:30px;
    text-align: center;
}
#returngoodsdata thead tr th{
	width:85px;
	text-align: center;
	border-right: 1px solid rgba(204, 204, 204, 0.39);
	background:#F2F3F7;
	box-shadow: 0px 2px 2px rgba(204, 204, 204, 0.58);
	
}
#returngoodsdata tbody tr td{
  	text-align: center;
  	border-right: 1px solid rgba(204, 204, 204, 0.39);
}
.sales{
    font-size: 18px;
    padding-left: 20px;
    padding-top: 10px;
}
.site-table tr td:nth-child(1){
	background:#EEEEEE;
}
.site-table tr td:nth-child(3){
	background:#EEEEEE;
}
.layui-input{
	height:30px;
	line-height:30px;
}
#returngoodsdata{
	width:98%;
	margin:0px auto;
}
#returngoodsdata thead tr th{
	padding:10px;
}
.site-table tbody tr td:nth-child(2){
	text-align: left;
	padding-left: 20px;
}
.site-table tbody tr td:nth-child(4){
	text-align: left;
	padding-left: 20px;
}
.site-table tbody tr .sellback-way{
	text-align: left;
    padding-left: 20px;
}
.sellback-word .layui-edge{right:38%;}
.sellback-way .layui-edge{right:40%;}
</style>

<div class="admin-main">
	<form method="post" id="theForm" class="layui-form">
		<input type="hidden" value="${orderinfo.member_id!'' }" name="member_id" />
		<input type="hidden" value="${orderinfo.order_id }" name="orderId" />
		<input type="hidden" value="1" name="status"/>
		<div class="shadowBoxWhite  whiteBox detail">
		  <div>
		  	<p class="sales">申请售后</p>
		  	<hr>
		  </div>
			<table class="layui-table site-table table-hover" cellspacing="0" cellpadding="0" border="0" style="width: 98%;margin:30px auto;">
				<tr>
					<th class="title" colspan="8" style="padding-left:20px;font-size:14px;color:red;background:#F2F3F7;box-shadow: 0px 2px 2px rgba(204, 204, 204, 0.58);
					">新建退货申请</th>
				</tr>
				<tr>
					<td align="right">退货单号：</td>
					<td><input type="hidden" name="tradeno" value="${orderinfo.sn }" /></td>
					<td>状态：</td>
					<td>申请退货</td>
				</tr>
				<tr>
					<td>关联订单号：</td>
					<td>${orderinfo.sn } 
						<input type="hidden" maxlength="50" name="ordersn" value="${orderinfo.sn }" /></td>
					<td>订单使用：</td>
					<td><span id="meta"></span> 已支付：￥${orderinfo.paymoney}</td>
					<input type="hidden"  id="paypricereturn" value="${orderinfo.paymoney!''}"/>
				</tr>

				<tr>
					<td>退货人：</td>
					<td>${orderinfo.ship_name } 
						<input type="hidden" name="sndto" value="${orderinfo.ship_name }">
					</td>
					<td>退款方式：</td>
						<input type="hidden" name = "paymenttype" value = "${orderinfo.payment_type}">
						<input type="hidden" value="${payCfg.is_retrace}" class="refund_way">
					<td class="sellback-way">
						<#if payCfg.is_retrace == 1>
							原支付方式返回
							<input type = "hidden" name="refund_way" id="original_way" value = "原支付方式退款">
						<#else>
							<select name="refund_way" id="way"> 
								<option value="0">-- 请选择退款方式 --</option>
								<#list paymentList as payment  >
									<#if payment.type!="cod">
										<option value="${payment.name}"> ${payment.name} </option>
									</#if>
								</#list>
							</select>
						</#if>
					</td>	
				</tr>
				<tr>
						
						<td>退货原因：</td>
						<td class="sellback-word">
							<select id="reason" name="reason" >
								<option value="0">请选择退货原因</option>
								<option value="商品质量有问题">商品质量有问题</option>
								<option value="收到商品与描述不符">收到商品与描述不符</option>
								<option value="不喜欢/不想要">不喜欢/不想要</option>
								<option value="发票问题">发票问题</option>
								<option value="空包裹">空包裹</option>
								<option value="快递无记录">快递无记录</option>
								<option value="快递一直没有收到">快递一直没有收到</option>
								<option value="买错/不想要">买错/不想要</option>
								<option value="未按照时间发货">未按照时间发货</option>
								<option value="其他">其他</option>
							</select>
						</td>
						<#if payCfg.is_retrace != 1>
						<td><div style="display:block;" id="account">退款账户：</div></td>
						<td>
							<input type="text" name="return_account" id="payment_account" class="layui-input" maxlength="255" />
						</td>
					</#if>
				</tr>
				<tr>
					
					<td>退款金额：</td>
					<td>
						<input type="text"  maxlength="8"  name="apply_alltotal" value="${orderinfo.order_amount }" class="layui-input" precision="2"/>
					</td>
					
				</tr>
				<tr>
					<td align="right">操作备注：</td>
					<td colspan="3" style="text-align:left;">
						<textarea type="text" style="width:500px;height: 60px;" class="layui-input" maxlength="125" value="" id="seller_remark" name="remark" placeholder="你最多可输入125个字" /></textarea>
					</td>
				</tr> 
			</table>
		</div>
		<br>
		<div class="shadowBoxWhite  whiteBox detail" >
			<table  id="returngoodsdata"  cellspacing="0" cellpadding="0" border="0" ">
				<input type="hidden" id="packDetailJson" name="packDetailJson"/>
				<thead>
					<tr>
						<th class="title" colspan="6" style="text-align:left;padding:10px 10px;font-size:14px;background:#F2F3F7;color:red;">请选择要退货的商品</th>
					</tr>
					<tr> 
					<th>是否退货</th> 
					<th>商品图片</th>
					<th>商品名称</th>
					<th>单价</th>
					<th>购买数量</th>
					<th>退货数量</th> 
					</tr>
				</thead>
				<#list orderItem as item>
					<tr>
							<td>
								<#if item.state==0 >
								<div class="layui-form-item">
									<input type="checkbox" name="goodsId" id= "chbgoodsId" value="${item.goods_id}" lay-skin="primary"/>
								</div>
									<input type="hidden" class="need_sync" goods_id="${item.goods_id}" name="item_id" value="${item.item_id}" autocomplete="off"/> 
									<input type="hidden" class="need_sync" goods_id="${item.goods_id}"  name="productId" value="${item.product_id}"  autocomplete="off"/>
									<input type="hidden" class="need_sync"  goods_id="${item.goods_id}" name="price" value="${item.price}"  autocomplete="off"/>
									<input type="hidden" class="need_sync" goods_id="${item.goods_id}" name="payNum" value="${item.num}" autocomplete="off"/> 
								
									
								<#else>
						 		&nbsp;&nbsp;
								</#if>
							</td>
							<td style="width:50px">
								<a target="_blank" href="${ctx}/goods-${item.goods_id}.html">
									<span style="display: block; height: 50px;padding:4px;" class="cart-product-img">
										<img height="50" src="${item.image!''}"  style="cursor: pointer;" />
									</span>
								</a>				
							</td>
							<td style="width:300px;"><a target="_blank" href="${ctx}/goods-${item.goods_id}.html">
							${item.name}
							
							<#if item.other?? >
								${item.other}
							</#if>
							</a></td>
							<td>${item.price?string.currency}</td>
							<td>
								${item.num}
							</td>
							<td>
								<input  name="goodsNum" onblur="checkRate()" onkeyup="this.value=this.value.replace(/[^0-9]+/g, '');" type="text" value="${item.num}" maxnum="${item.num}" goods_id="${item.goods_id}" class="input_text"  style="width:30px;border:none;text-align:center;" autocomplete="off" />
							</td> 
						</tr> 
					</#list>
					<#if (gift.gift_id)??>
						<tr>
							<td>
								<#if gift.gift_status == 0>
									<input type="checkbox" name="gift_id" value="${gift.gift_id!''}" disabled="disabled" checked="checked"/>
								<#else>
						 		&nbsp;&nbsp;
								</#if>
							</td>
							<td style="width:50px">
								<span style="display: block; height: 50px;padding:4px;" class="cart-product-img">
									<img height="50" width="50" src="${gift.gift_img!''}"/>
								</span>
							</td>
							<td style="width:300px;">${gift.gift_name!''}<span style="color: red; ">（赠品，必须退回）</span></td>
							<td>${gift.gift_price?string.currency}</td>
							<td>1</td>
							<td><input type="text" value="1" disabled="disabled" class="input_text" style="width:30px"/></td>
						</tr>
					</#if>
			</table>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
			<button class="layui-btn" id="subapply" lay-submit="" lay-filter="subapply">提交申请</button>
			</div>
		</div>
	</form>
	
	
</div>

<script type="text/javascript">

//获取支付方式
var pay_way=$(".refund_way").val();

/**
 * 检测退货数量是否是数字 否则返回
 */
function checkRate(input){
    var nubmer = $("[name=goodsNum]").val();
    if(isNaN(nubmer)){
    	$("[name=goodsNum]").val(0);
    }
}

$(function(){
	
	$("input[name='goodsId']").click(function(){
		var checked = this.checked;
		var goodsid  = $(this).val();
		if(checked){
			$(".need_sync[goods_id="+goodsid+"]").attr("checked","checked");
			$("[name=goodsNum][goods_id="+goodsid+"]").removeAttr("disabled");
		}else{
			$(".need_sync[goods_id="+goodsid+"]").removeAttr("checked");
			$("[name=goodsNum][goods_id="+goodsid+"]").attr("disabled","disabled");
		}
	});
	
	/**暂时去掉此处代码，并没有实质的作用*/
	$.getJSON("${ctx}/shop/admin/sell-back/search-sn.do?orderId=${orderinfo.order_id }",function(data){
		if(data.result == 1){
			$.Loading.error(data.message);
			parent.reloadTabGrid("退货单"); 					
			parent.CloseTabByTitle("申请售后");
		}
	});
	
}); 

layui.use('form', function(){
	
	  var form = layui.form();
	  form.render(); //更新全部
	  form.on('submit(subapply)', function(data) {
		  //选择商品的验证
		  if($("input[name='goodsId']").filter(":checked").length==0){
				alert("请选择要退货的商品");
				return false;
			}
			
		  	//退款账号的验证
		  if(pay_way != 1){
				if($("#payment_account").val() == ""){
					alert("退款账号不能为空");
					return false;
				}
				if($("#way").val() ==0){
					alert("退款方式不能为空!");
					return false;
				}
			}
			 
		//退款原因的验证
			if($("#reason").val()=="0"){
				alert("请选择退货原因");
				return false;	
			}
		
			//退款金额
			var return_money=$("input[name='apply_alltotal']").val();
			if(pay_way == 1){
				if( Number(return_money) == "0"){
					alert("退款金额不能为零");
					$("#subapply").removeAttr("disabled");
					return false;
				}
			}
			if(isNaN(return_money)){
				alert("退款金额必须是浮点型数字，例如1.00");
				$("#subapply").removeAttr("disabled");
				return false;
			}
			if( return_money== ""){
				alert("退款金额不能为空");
				$("#subapply").removeAttr("disabled");
				return false;
			}
			if( Number(return_money) < "0"){
				alert("退款金额不能为负数");
				$("#subapply").removeAttr("disabled");
				return false;
			}
			if( Number(return_money) > Number($("#paypricereturn").val())){
				alert("退款金额不能大于实付金额");
				$("#subapply").removeAttr("disabled");
				return false;
			}
			
		
			var error =0;
			
			$("[name=goodsNum]").each(function(i,v){
				var $this = $(v);
				var maxnum= $this.attr("maxnum");
				var num = $this.val();
				
				if(num==""){
					alert("请输入退货数量");
					error=1;
					return ;
				}
				
				
				if(num=="0"){
					alert("退货数量不能为0");
					error=1;
					return ;
				} 
				
				if(parseInt(num)>parseInt(maxnum)){
					alert("退货数量不能大于购物数量");
					error=1;
					return ;
				}
				
			});
			
			
			if(1==error){
				return false;
			}
			
			
		  var options = {
					url : "${ctx}/shop/admin/sell-back/apply.do",
					type : "POST",
					dataType : "json",
					async : false,
					success : function(result) {
						if(result.result==1){
							$.Loading.success(result.message);
							$("#subapply").addClass("layui-btn-disabled");
							closeCurrent();
						}else{
							$.Loading.error(result.message);
						}
						},
				 	error : function(e) {
				 		$.Loading.error('出现错误，请重试');
				 		}
				};
				$('#theForm').ajaxSubmit(options);
				return false;
	});
});
</script>
<#include '/admin/footer.html' >
